package xyz.yuzegod.megawalls.spectator;

import org.bukkit.GameMode;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.game.GamePlayer;

import java.text.DecimalFormat;

public class SpectatorTarget
{
    private final DecimalFormat df;
    private GamePlayer gamePlayer;
    private GamePlayer gameTarget;
    private Player player;
    private Player target;
    
    public SpectatorTarget(final GamePlayer gamePlayer, final GamePlayer gameTarget) {
        this.df = new DecimalFormat("0.0");
        this.gamePlayer = gamePlayer;
        this.player = gamePlayer.getPlayer();
        this.gameTarget = gameTarget;
    }
    
    public GamePlayer getPlayer() {
        return this.gamePlayer;
    }
    
    public GamePlayer getTarget() {
        return this.gameTarget;
    }
    
    public void setTarget(final GamePlayer gameTarget) {
        this.gameTarget = gameTarget;
        this.target = gameTarget.getPlayer();
    }
    
    public void sendTip() {
        if (!this.check()) {
            return;
        }
        if (this.player.getSpectatorTarget() != null && this.player.getSpectatorTarget().equals(this.target)) {
            this.gamePlayer.sendActionBar(this.formatSpectatorTip(this.player, this.target, true) + "  §a点击左键打开菜单  §c按Shift退出");
            return;
        }
        if (!this.player.getWorld().equals(this.target.getWorld())) {
            this.gamePlayer.sendActionBar("§c§l目标已丢失或不在同一个世界");
            return;
        }
        this.gamePlayer.sendActionBar(this.formatSpectatorTip(this.player, this.target, false));
    }
    
    public void autoTp() {
        if (!this.check()) {
            return;
        }
        if (SpectatorSettings.get(this.gamePlayer).getOption(SpectatorSettings.Option.AUTOTP) && (!this.player.getWorld().equals(this.target.getWorld()) || this.player.getLocation().distance(this.target.getLocation()) >= 20.0)) {
            this.player.teleport((Entity)this.target);
            if (SpectatorSettings.get(this.gamePlayer).getOption(SpectatorSettings.Option.FIRSTPERSON)) {
                this.gamePlayer.sendTitle("§a正在旁观§7" + this.target.getName(), "§a点击左键打开菜单  §c按Shift键退出", 0, 20, 0);
                this.player.setGameMode(GameMode.SPECTATOR);
                this.player.setSpectatorTarget((Entity)this.target);
            }
        }
    }
    
    public void tp() {
        if (!this.check()) {
            return;
        }
        if (SpectatorSettings.get(this.gamePlayer).getOption(SpectatorSettings.Option.FIRSTPERSON)) {
            this.player.teleport((Entity)this.target);
            this.gamePlayer.sendTitle("§a正在旁观§7" + this.target.getName(), "§a点击左键打开菜单  §c按Shift键退出", 0, 20, 0);
            this.player.setGameMode(GameMode.SPECTATOR);
            this.player.setSpectatorTarget((Entity)this.target);
            return;
        }
        this.player.teleport((Entity)this.target);
    }
    
    public boolean check() {
        if (this.gameTarget == null || this.target == null) {
            return false;
        }
        if (this.gameTarget.isSpectator() || !this.target.isOnline()) {
            this.gameTarget = null;
            this.target = null;
            return false;
        }
        return true;
    }
    
    private String formatSpectatorTip(final Player player, final Player target, final boolean firstMode) {
        final GamePlayer targetPlayer = GamePlayer.get(target.getUniqueId());
        if (firstMode) {
            return "§f目标: §a§l" + target.getName() + "  §f生命值: §a§l" + (int)target.getHealth() + " §c§l❤  §f职业: §a§l" + targetPlayer.getPlayerStats().getSelected().getDisplayName();
        }
        final String distance = this.df.format(player.getLocation().distance(target.getLocation()));
        return "§f目标: §a§l" + target.getName() + "  §f生命值: §a§l" + (int)target.getHealth() + " §c§l❤  §f职业: §a§l" + targetPlayer.getPlayerStats().getSelected().getDisplayName() + "  §f距离: §a§l" + distance + "米";
    }
}
